﻿using MVC_No05.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_No05.Controllers
{
    public class MyHomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            using (Model1 model = new Model1())
            {
                return View(model.Task.Where(u => true).ToList());
            }
        }

        [HttpGet]
        public ActionResult Create()
        {
            var TaskState = new List<SelectListItem>();
            TaskState.Add(new SelectListItem()
            {
                Value = "1",
                Text = "未完成",
            });
            TaskState.Add(new SelectListItem()
            {
                Value = "2",
                Text = "已完成",
            });
            ViewBag.TaskState = TaskState;


            var TaskPriority = new List<SelectListItem>();
            TaskPriority.Add(new SelectListItem()
            {
                Value = "1",
                Text = "高",
            });
            TaskPriority.Add(new SelectListItem()
            {
                Value = "2",
                Text = "中",
            });
            TaskPriority.Add(new SelectListItem()
            {
                Value = "3",
                Text = "低",
            });
            ViewBag.TaskPriority = TaskPriority;


            return View(new Task());
        }

        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                using (Model1 model = new Model1())
                {
                    model.Task.Add(task);
                    model.SaveChanges();
                }
                return RedirectToAction("Index", "MyHome");
            }
            else
                return RedirectToAction("Create", "MyHome");
        }

        [HttpPost]
        public ActionResult Delete(int uid)
        {
            using (Model1 model = new Model1())
            {
                model.Task.Remove(model.Task.FirstOrDefault(u => u.TaskID == uid));
                return Json(model.SaveChanges());
            }
        }

        [HttpGet]
        public ActionResult XiangQing(int uid = 0)
        {
            if (uid == 0)
            {
                return RedirectToAction("Index", "MyHome");
            }
            else
            {
                using (Model1 model = new Model1())
                {
                    return View(model.Task.FirstOrDefault(u => u.TaskID == uid));
                }
            }
        }

        [HttpGet]
        public ActionResult Update(int uid = 0)
        {
            if (uid == 0)
            {
                return RedirectToAction("Index", "MyHome");
            }
            else
            {
                using (Model1 model = new Model1())
                {
                    var TaskState1 = new List<SelectListItem>();
                    TaskState1.Add(new SelectListItem()
                    {
                        Value = "1",
                        Text = "未完成",
                    });
                    TaskState1.Add(new SelectListItem()
                    {
                        Value = "2",
                        Text = "已完成",
                    });

                    var TaskPriority1 = new List<SelectListItem>();
                    TaskPriority1.Add(new SelectListItem()
                    {
                        Value = "1",
                        Text = "高",
                    });
                    TaskPriority1.Add(new SelectListItem()
                    {
                        Value = "2",
                        Text = "中",
                    });
                    TaskPriority1.Add(new SelectListItem()
                    {
                        Value = "3",
                        Text = "低",
                    });


                    var data = model.Task.FirstOrDefault(u => u.TaskID == uid);
                    if (int.TryParse(data.TaskState.ToString(), out int i))
                    {
                        TaskState1[Convert.ToInt32(data.TaskState) - 1].Selected = true;
                    }
                    TaskPriority1[Convert.ToInt32(data.TaskPriority) - 1].Selected = true;

                    ViewBag.TaskState1 = TaskState1;
                    ViewBag.TaskPriority1 = TaskPriority1;

                    return View(data);
                }
            }
        }

        [HttpPost]
        public ActionResult UpdateData(Task task)
        {
            if (ModelState.IsValid)
            {
                using (Model1 model = new Model1())
                {
                    var data = model.Task.FirstOrDefault(u => u.TaskID == task.TaskID);
                    if (data != null)
                    {
                        data.TaskPriority = task.TaskPriority;
                        data.Content = task.Content;
                        data.AddTime = task.AddTime;
                        data.TaskPriority = task.TaskPriority;
                        data.FinishedTime = task.FinishedTime;
                        if (model.SaveChanges() == 1)
                        {
                            return RedirectToAction("Index", "MyHome");
                        }
                    }
                }
                return RedirectToAction("Update", "MyHome");
            }
            else
                return RedirectToAction("Update", "MyHome");
        }
    }
}